﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace GraphosData
{


	public class GraphReader
	{

		private Graph graph;
		private Person person;
		private Person personOther;

		private XDocument xDoc;
		private XElement xRoot;

		private string strId = "id";
		private string strFirstName = "firstname";
		private string strLastName = "lastname";
		private string strGender = "gender";
		private string strRel = "relationships";
		private string strRootName;
		private string strFileName;

		Dictionary<string, Person> dictPeople;
		Relation relationType;


		//constructors
		public GraphReader(string filename)
		{
			strFileName = filename;
			dictPeople = new Dictionary<string, Person>();
			graph = new Graph();
		}

		public GraphReader()
		{
			strRootName = "people";
			graph = new Graph();
			dictPeople = new Dictionary<string, Person>();
		}

		//public functions
		public Graph OpenFile(string filename, string rootname)
		{
			try
			{
				xDoc = XDocument.Load(filename);
			}
			catch
			{
				return null;
			}
			xRoot = xDoc.Element(rootname);

			foreach (XElement p in xRoot.Elements())
			{
				person = new Person();
				person.FirstName = (string)p.Element(strFirstName);
				person.ID = (string)p.Element(strId);
				person.LastName = (string)p.Element(strLastName);
				person.Gender = (string)p.Element(strGender) == "M" ? Gender.Male : Gender.Female;

				dictPeople[person.ID] = person;
				graph.AddNode(person);
			}

			foreach (XElement p in xRoot.Elements())
			{
				person = dictPeople[(string)p.Element(strId)];

				foreach (XElement relatedpeople in p.Element(strRel).Elements())
				{
					personOther = dictPeople[(string)relatedpeople.Element(strId)];

					string relType = (string)relatedpeople.Element("type");
					switch (relType)
					{

						case "son":
							relationType = Relation.Son;
							break;
						case "daughter":
							relationType = Relation.Daughter;
							break;
						case "mother":
							relationType = Relation.Mother;
							break;
						case "father":
							relationType = Relation.Father;
							break;
						case "sister":
							relationType = Relation.Sister;
							break;
						case "brother":
							relationType = Relation.Brother;
							break;
						case "wife":
							relationType = Relation.Wife;
							break;
						case "husband":
							relationType = Relation.Husband;
							break;
						case "apostle":
							relationType = Relation.Apostle;
							break;
						case "grandson":
							relationType = Relation.Grandson;
							break;
						case "family":
							relationType = Relation.Family;
							break;
						case "maidservent":
							relationType = Relation.MaidServent;
							break;
						case "concubine":
							relationType = Relation.Concubine;
							break;
						default:
							relationType = Relation.Unknown;
							break;
					}
					graph.AddEdge(person, personOther, relationType);
				}
			}
			return graph;
		}

		public Graph OpenFile(string filename)
		{
			return OpenFile(filename, strRootName);
		}


		//properties
		public string RootName
		{
			get { return strRootName; }
			set { strRootName = value; }
		}

	}
}
